<?php
namespace Api\Controller;

use ZL\Curl;

class WifiController extends BaseController
{
    public function savePost()
    {
        if(IS_POST){
            $data = json_decode($_POST['data'],true);
            $sniffer = D('Api/Sniffer')->checkAndSave($this->getSniffer($data));
            foreach ($data['data'] as $item) {
                $phone = D('Phone')->checkAndSave($this->getPhone($item));
                $log = $this->getLog($item);
                $log['dev_id'] = $sniffer['id'];
                $log['phone_id'] = $phone['id'];
                $log = D('Log')->add(checkArr($log));
            }
        }
    }
    
    protected function getSniffer($_data){
        $data["dev_id"] = 	$_data["id"];
        $data["mmac"] = $_data["mmac"];
        $data["rate"] = $_data["rate"];
        $data["wssid"] = $_data["wssid"];
        $data["wmac"] = $_data["wmac"];
        $data["created_at"] = date('Y-m-d H:i:s',strtotime($_data["time"]));
        return $data;
    }

    protected function getPhone($_data)
    {
        $data["mac"] = $_data["mac"];
        $data["rssi"] = $_data["rssi"];
        $data["ds"] = $_data["ds"]?$_data["ds"]:'N';
        $data["ts"] = $_data["ts"];
        $data["tc"] = $_data["tc"]?$_data["tc"]:'N';
        $data["essid0"] = $_data["essid0"];
        $data["essid1"] = $_data["essid1"];
        $data["essid2"] = $_data["essid2"];
        return $data;
    }
    protected function getLog($_data)
    {
        $data["rssi"] = $_data["rssi"];
        $data["ds"] = $_data["ds"];
        $data["tc"] = $_data["tc"];
        $data["ts"] = $_data["ts"];
        $data["essid0"] = $_data["essid0"];
        $data["essid1"] = $_data["essid1"];
        $data["essid2"] = $_data["essid2"];
        return $data;
    }



    ////==========================================测-试===================================================
    public function test()
    {
        $curl = new Curl();

        $str1 = json_encode(['mac'=>21212121212121]);
        $data['data'] = $str1;
//        $data['data'] = $str;
        dump($curl->curl('mac.cc/mac.php',$data,true));
    }

    public function testSave()
    {

        $data=array();
        $data["dev_id"] = 	121;
        $data["mmac"] =  121;
        $data["rate"] =    121;
        $data["wssid"] = 121;
        $data["wmac"] = 121;
        $data["created_at"] = date('Y-m-d H:i:s',time());
        $data["updated_at"] = date('Y-m-d H:i:s',time());
        $data = D('Api/Sniffer')->create($data);
        var_dump(D('Api/Sniffer')->data($data)->add());
    }
    public function testSave1()
    {
        $data["rssi"] = -67;
        $data["phone_id"] = 1;
        $data["dev_id"] = 3;
        $data["ds"] = null;
        $data["tc"] = null;
        $data["ts"] = null;
        $data["essid0"] = null;
        $data["essid1"] = null;
        $data["essid2"] = null;
        $data = D('Api/Log')->create($data);
        var_dump(D('Api/Log')->data(checkArr($data))->add());
    }
}
